package org.shreker.tools.toolbox;

import java.io.File;
import org.shreker.tools.utils.compress.ZIPUtil;

/**
 * @author ShrekerNil
 */
public class Rename {
    
    public static void main(String[] args) {
        String filePath = "D:\\Downloads\\BaiduYun\\Spark从入门到精通";
        //rename(filePath);
        renameMultiFiles(filePath);
        //renameAll(filePath);
        //compress();
    }
    
    private static void rename(String filePath) {
        File root = new File(filePath);
        File[] files = root.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                String finalFile = lastComponentName(file.getPath());
                String path = file.getPath();
                String filename = lastComponentName(path);
                File[] subfiles = file.listFiles();
                boolean hasText = false;
                File file1 = null;
                for (File subfile : subfiles) {
                    file1 = subfile;
                    String lastComponentName = lastComponentName(subfile.getPath());
                    if (lastComponentName.equals("视频")) {
                        hasText = true;
                        File[] listFiles = subfile.listFiles();
                        if (listFiles.length != 1) {
                            System.out.println(listFiles.length);
                            for (File listFile : listFiles) {
                                String path1 = listFile.getPath();
                                String substring = path1.substring(path1.lastIndexOf('.') + 1);
                                if (!substring.equals("avi")) {
                                    System.out.println("******************************");
                                } else {
                                    String parent = listFile.getPath().substring(0, listFile.getPath().lastIndexOf('\\'));
                                    String finalName = parent + "\\" + finalFile + ".avi";
                                    boolean success = listFile.renameTo(new File(finalName));
                                    if (!success) {
                                        System.out.println("重命名失败：" + finalName);
                                    }
                                }
                            }
                        }
                    }
                }
                if (!hasText) {
                    System.out.println("File" + file1);
                }
            } else {
                System.out.println("File: " + file);
            }
        }
    }
    
    private static void renameMultiFiles(String filePath) {
        File root = new File(filePath);
        File[] files = root.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                String finalFile = lastComponentName(file.getPath());
                String path = file.getPath();
                String filename = lastComponentName(path);
                File[] subfiles = file.listFiles();
                boolean hasText = false;
                for (File subfile : subfiles) {
                    String lastComponentName = lastComponentName(subfile.getPath());
                    if (lastComponentName.equals("视频")) {
                        File[] listFiles = subfile.listFiles();
                        int length = listFiles.length;
                        if (length > 1) {
                            System.out.println("length:" + length);
                        }
                        for (File listFile : listFiles) {
                            if (length == 1) {
                            
                            } else if (length > 1) {
                                System.out.println("文件：" + listFile);
                            } else {
                                System.out.println("空文件夹：");
                            }
                        }
                    }
                }
            }
        }
    }
    
    private static void renameAll(String filePath) {
        File root = new File(filePath);
        File[] files = root.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                String finalFile = lastComponentName(file.getPath());
                String path = file.getPath();
                String filename = lastComponentName(path);
                File[] subfiles = file.listFiles();
                boolean hasText = false;
                for (File subfile : subfiles) {
                    String lastComponentName = lastComponentName(subfile.getPath());
                    if (lastComponentName.equals("视频")) {
                        File[] listFiles = subfile.listFiles();
                        for (File listFile : listFiles) {
                            String temp = listFile.getPath();
                            if (temp.contains("_转")) {
                                temp = temp.replace("_转", "");
                            }
                            boolean success = listFile.renameTo(new File(temp));
                            if (!success) {
                                System.out.println("文件重命名失败：" + listFile);
                            }
                        }
                    }
                }
            }
        }
    }
    
    private static void compress(String filePath) {
        File root = new File(filePath);
        File[] files = root.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                System.out.println("正在压缩：" + file);
                ZIPUtil.compress(file.getPath(), file.getPath() + ".zip");
            }
        }
    }
    
    private static String lastComponentName(String path) {
        return path.substring(path.lastIndexOf('\\') + 1);
    }
    
}
